package com.tsingteng.cosfun.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tsingteng.cosfun.R;

//非wifi提示
public class ShowNetStatusDialog extends Dialog {
    public ShowNetStatusDialog(Context context) {
        super(context);
    }

    public ShowNetStatusDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private OnItemClickListener onItemClickListener;
        String contentStr;
        String buttonStr;
        int type;
        private View layout;
        private ShowNetStatusDialog dialog;

        public Builder(Context context, Activity activity) {
            //这里传入自定义的style，直接影响此Dialog的显示效果。style具体实现见style.xml
            dialog = new ShowNetStatusDialog(context, R.style.commselectDialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.net_status_dialog, null);
            dialog.addContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        }

        public Builder setItemClickListener(OnItemClickListener listener) {
            this.onItemClickListener = listener;
            return this;
        }

        public Builder setMessage(int type, String contentStr, String buttonStr) {
            this.contentStr = contentStr;
            this.buttonStr = buttonStr;
            this.type = type;
            return this;
        }

        /**
         * 创建单按钮对话框
         *
         * @return
         */
        public ShowNetStatusDialog createDialog() {
            TextView content = layout.findViewById(R.id.net_status_dialog_content);
            TextView next = layout.findViewById(R.id.net_status_dialog_next);
            content.setText(contentStr);
            next.setText(buttonStr);
            next.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    if (type == 0) {
                        onItemClickListener.onPlay();
                    } else if (type == 1) {
                        onItemClickListener.onNetRefresh();
                    } else {
                        onItemClickListener.onRefreshPlay();
                    }
//                        dialog.dismiss();
                }
            });

            create();
            return dialog;
        }

        /**
         * 单按钮对话框和双按钮对话框的公共部分在这里设置
         */
        private void create() {
            dialog.setContentView(layout);
            dialog.setCancelable(true);     //用户可以点击手机Back键取消对话框显示
            dialog.setCanceledOnTouchOutside(true);        //用户不能通过点击对话框之外的地方取消对话框显示
        }

    }


    public interface OnItemClickListener {
        void onPlay();

        void onRefreshPlay();

        void onNetRefresh();
    }
}
